package com.delio.client.contabilidad.servicios;

import java.util.Date;

import com.delio.client.contabilidad.adelantoservicios.PanelNuevoAdelantoServicio;
import com.delio.client.idioma.DelioConstants;
import com.delio.client.util.ComboboxFieldBinding;
import com.delio.client.util.Utilities;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;

public class PanelNuevoServicio extends FormPanel {
	private DelioConstants k = GWT.create(DelioConstants.class);
	private FormBinding formbinding;
	private HiddenField<String> id;
	private Button btnRegistrar;
	private FormButtonBinding formbuttonbinding;

	private NumberField txtCantidad;
	private NumberField txtPunitario;
	private TextArea txtDescripcion;
	private DateField fecha;
	private ComboBox<BeanModel> cmbProveedor;
	private ComboBox<BeanModel> cmbArticulo;

	public PanelNuevoServicio() {
		super();
		setScrollMode(Scroll.AUTOY);
		setHeading(k.nuevoServicio());
		setFrame(true);
		setLabelAlign(LabelAlign.TOP);

		PanelGridServicio.panelnuevoservicio = this;
		formbinding = new FormBinding(this);

		btnRegistrar = new Button(k.registrar(),
				IconHelper.createPath("images/icons/save_all.png"));
		btnRegistrar.setSize(80, 30);
		btnRegistrar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				//save();
				new PanelNuevoAdelantoServicio().show();
			}
		});

		formbuttonbinding = new FormButtonBinding(this);
		//formbuttonbinding.addButton(btnRegistrar);

		Servicios.getPaginatecomboArticulos();
		Servicios.loadercombo.load();

		cmbArticulo = new ComboBox<BeanModel>();

		cmbArticulo.setFieldLabel(k.articulos());
		cmbArticulo.setEmptyText(k.buscar());
		cmbArticulo.setDisplayField("descripcion");
		cmbArticulo.setStore(Servicios.storecombo);
		cmbArticulo.setTriggerAction(TriggerAction.ALL);
		cmbArticulo.setValueField("descripcion");
		cmbArticulo.setAllowBlank(false);
		cmbArticulo.setUseQueryCache(false);
		cmbArticulo.setPageSize(10);

		Servicios.getPaginatecomboProveedores();
		Servicios.loadercombo2.load();

		cmbProveedor = new ComboBox<BeanModel>();
		cmbProveedor.setFieldLabel(k.razonSocial());
		cmbProveedor.setEmptyText(k.buscar());
		cmbProveedor.setDisplayField("razonsocial");
		cmbProveedor.setStore(Servicios.storecombo2);
		cmbProveedor.setTriggerAction(TriggerAction.ALL);
		cmbProveedor.setValueField("razonsocial");
		cmbProveedor.setAllowBlank(false);
		cmbProveedor.setEditable(false);
		cmbProveedor.setUseQueryCache(false);
		cmbProveedor.setPageSize(10);

		id = new HiddenField<String>();

		fecha = new DateField();
		fecha.setAllowBlank(false);
		fecha.setFieldLabel(k.fecha());
		fecha.setValue(new Date());

		txtCantidad = new NumberField();
		txtCantidad.setFieldLabel(k.cantidad());
		txtCantidad.setPropertyEditorType(Integer.class);
		txtCantidad.setAllowBlank(false);

		txtPunitario = new NumberField();
		txtPunitario.setFieldLabel(k.precio());
		txtPunitario.setPropertyEditorType(Double.class);
		txtPunitario.setAllowBlank(false);

		txtDescripcion = new TextArea();
		txtDescripcion.setHeight(40);
		txtDescripcion.setEmptyText(k.escribaobservacion());
		txtDescripcion.addListener(Events.KeyDown, new KeyListener() {

			public void componentKeyDown(ComponentEvent event) {
				Integer c = event.getKeyCode();
				if (c == KeyCodes.KEY_ENTER)
					btnRegistrar.focus();

			}

		});

		Utilities.focoEnter(cmbArticulo, cmbProveedor);
		Utilities.focoEnter(cmbProveedor, txtCantidad);
		Utilities.focoEnter(txtCantidad, txtPunitario);
		Utilities.focoEnter(txtPunitario, fecha);
		Utilities.focoEnter(fecha, txtDescripcion);

		LayoutContainer panel = new LayoutContainer();
		panel.setLayout(new ColumnLayout());

		LayoutContainer lc1 = new LayoutContainer();
		lc1.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc2 = new LayoutContainer();
		lc2.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc3 = new LayoutContainer();
		lc3.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc4 = new LayoutContainer();
		lc4.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc5 = new LayoutContainer();
		lc5.setLayout(new FormLayout(LabelAlign.TOP));

		LayoutContainer lc6 = new LayoutContainer();
		lc6.setStyleAttribute("marginTop", "4px");
		lc6.setStyleAttribute("marginLeft", "30px");

		lc1.add(cmbArticulo, new FormData("90%"));
		lc2.add(cmbProveedor, new FormData("90%"));
		lc3.add(txtCantidad, new FormData("70%"));
		lc4.add(txtPunitario, new FormData("70%"));
		lc5.add(fecha, new FormData("90%"));
		lc6.add(btnRegistrar);

		add(id);
		panel.add(lc1, new ColumnData(.20));
		panel.add(lc2, new ColumnData(.20));
		panel.add(lc3, new ColumnData(.08));
		panel.add(lc4, new ColumnData(.10));
		panel.add(lc5, new ColumnData(.10));
		panel.add(txtDescripcion, new ColumnData(.20));
		panel.add(lc6, new ColumnData(.08));
		add(panel);

		setStore(Servicios.store);

	}

	public void setStore(ListStore<BeanModel> store) {

		formbinding.addFieldBinding(new FieldBinding(id, "id"));
		formbinding.addFieldBinding(new FieldBinding(txtCantidad, "cantidad"));
		formbinding.addFieldBinding(new FieldBinding(txtDescripcion,
				"descripcion"));
		formbinding.addFieldBinding(new FieldBinding(txtPunitario,
				"preciounitario"));
		formbinding.addFieldBinding(new ComboboxFieldBinding(cmbArticulo,
				"articulo"));
		formbinding.addFieldBinding(new ComboboxFieldBinding(cmbProveedor,
				"proveedor"));
		formbinding.setStore(store);

	}

	@Override
	public void reset() {
		id.clear();
		txtCantidad.clear();
		txtDescripcion.clear();
		txtPunitario.clear();

		PanelGridServicio.grid.getSelectionModel().deselectAll();
		unbind();

	}

	public void bind(BeanModel selectedModel) {
		formbinding.bind(selectedModel);
		setHeading(k.editarServicio());

	}

	public void unbind() {
		formbinding.unbind();
		setHeading(k.nuevoServicio());
	}

	public void save() {

		Integer cantidad = txtCantidad.getValue().intValue();
		Double preciounitario = txtPunitario.getValue().doubleValue();
		String descripcion = txtDescripcion.getValue();

		Servicio serv = new Servicio();

		if (id.getValue() != null) {
			serv.setId(Integer.valueOf(id.getValue().toString()));
		}

		serv.setCantidad(cantidad);
		serv.setDescripcion(descripcion);
		serv.setPreciounitario(preciounitario);

		if (cmbArticulo.getValue().get("id") != null)
			serv.setArticulo_id(Integer.valueOf(cmbArticulo.getValue()
					.get("id").toString()));

		if (cmbProveedor.getValue().get("id") != null)
			serv.setProveedor_id(Integer.valueOf(cmbProveedor.getValue()
					.get("id").toString()));

		serv.setFecha(fecha.getValue());

		Servicios.save(serv);
		Servicios.panelnuevoservicio = this;

	}
}
